package com.example.revise

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.example.revise.databinding.ActivityVideoPlayerBinding

class VideoPlayerActivity : AppCompatActivity() {
    private lateinit var binding: ActivityVideoPlayerBinding

    companion object {
        private const val EXTRA_VIDEO_RES_ID = "video_res_id"
        private const val EXTRA_TITLE = "title"
        private const val EXTRA_DESCRIPTION = "description"
        
        fun createIntent(context: Context, videoResId: Int, title: String, description: String): Intent {
            return Intent(context, VideoPlayerActivity::class.java).apply {
                putExtra(EXTRA_VIDEO_RES_ID, videoResId)
                putExtra(EXTRA_TITLE, title)
                putExtra(EXTRA_DESCRIPTION, description)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityVideoPlayerBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Set full screen
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)

        // Get video resource ID and metadata
        val videoResId = intent.getIntExtra(EXTRA_VIDEO_RES_ID, 0)
        val title = intent.getStringExtra(EXTRA_TITLE) ?: ""
        val description = intent.getStringExtra(EXTRA_DESCRIPTION) ?: ""
        
        if (videoResId == 0) {
            finish()
            return
        }

        // Set title and description
        binding.titleTextView.text = title
        binding.descriptionTextView.text = description

        // Set up video
        val uri = Uri.parse("android.resource://$packageName/$videoResId")
        binding.fullscreenVideoView.apply {
            setVideoURI(uri)
            setOnPreparedListener { mediaPlayer ->
                mediaPlayer.isLooping = true
                start()
            }
        }

        // Set up close button
        binding.closeButton.setOnClickListener {
            finish()
        }
    }

    override fun onPause() {
        super.onPause()
        binding.fullscreenVideoView.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.fullscreenVideoView.stopPlayback()
    }
} 